package dongze.com.ckdj.service.uploader;

import android.util.Log;

import java.io.File;
import java.io.IOException;

import dongze.com.ckdj.data.DataLayer;
import dongze.com.ckdj.data.entity.request.UploadFileRequestBody;
import dongze.com.ckdj.data.entity.response.UploadImageResponse;
import dongze.com.ckdj.persistence.dao.Image;
import dongze.com.ckdj.persistence.dao.ImageDao;
import dongze.com.ckdj.persistence.dao.State;
import dongze.com.ckdj.util.MyLog;
import io.reactivex.ObservableEmitter;
import io.reactivex.Scheduler;
import retrofit2.Response;


/**
 * Created by fanqwz on 2017/8/21.
 */

public class ImageUploader extends Uploader<String, Object> {
    private static final String TAG = ImageUploader.class.getSimpleName();
    DataLayer dataLayer;
    Image image;
    ImageDao dao;
    ImageProgressListener listener;

    public ImageUploader(Scheduler subscribeThread, Scheduler observerThread, DataLayer dataLayer, ImageDao dao, Image image, ImageProgressListener listener) {
        super(subscribeThread, observerThread);
        this.dataLayer = dataLayer;
        this.dao = dao;
        this.image = image;
        this.listener = listener;
    }

    @Override
    Worker getWorker() {
        return worker;
    }

    Worker<String, Object> worker = new Worker<String, Object>() {
        @Override
        public void accept(Object depends) {

        }

        @Override
        public void doWork(ObservableEmitter<String> e) {
            MyLog.d(TAG, "doWork: start upload image " + image.getFilePath());
            try {
                image.setState(State.Uploading);
                dao.update(image);

                if (listener != null)
                    listener.onStart();

                Response<UploadImageResponse> response = dataLayer.uploadImage(new File(image.getFilePath()), new UploadFileRequestBody.ProgressListener() {
                    @Override
                    public void onProgress(long written, long total, boolean finished) {
                        if (listener != null)
                            listener.onPregress((int) (100f * written / total));
                    }
                }).execute();

                if (response.code() == 200) {
                    if (response.body() != null && response.body().getPath() != null && !response.body().getPath().isEmpty()) {
                        image.setServerPath(response.body().getPath());
                        image.setState(State.Finished);
                        dao.update(image);

                        if (listener != null)
                            listener.onFinish();

                        e.onNext(response.body().getPath());
                        e.onComplete();
                    } else {
                        if (listener != null)
                            listener.onFailed();

                        e.onError(new Exception("server return null or empty"));
                    }
                } else {
                    if (listener != null)
                        listener.onFailed();

                    e.onError(new Exception("request return " + response.code()));
                }
            } catch (IOException e1) {
                MyLog.e(TAG, "doWork: ", e1);
                if (listener != null)
                    listener.onFailed();

                e.onError(e1);
            }
        }
    };

    public interface ImageProgressListener {
        void onStart();
        void onPregress(int progress);
        void onFinish();
        void onFailed();

    }
}
